<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Nats\Consumer;
use Hyperf\Nats\AbstractConsumer;
use Hyperf\Nats\Annotation\Consumer;
use Hyperf\Nats\Message;
use App\Utils\ServerUtil;
use App\Logic\Channel;

/**
 *
 */
class ChannelConsumer extends AbstractConsumer
{
    public function consume(Message $payload)
    {
        $data = $payload->getBody(); 
        $data = json_decode($data,true);
        ServerUtil::info('Nats获取渠道信息', ['path'=>'sg_phonecode.channel', 'parameter'=>$data]);
        $type = empty($data['type']) ? 1 : $data['type'];
        $obj = $this->container->get(Channel::class);

        //开协程处理,防止进程阻塞下个逻辑
        co(function () use ($obj, $payload, $type, $data){
            switch ($type)
            {
                case 1:
                    //随机获取渠道信息
                    $rechargeType = empty($data['recharge_type']) ? 0 : (int)$data['recharge_type'];
                    $operatorType = empty($data['operator_type']) ? 0 : (int)$data['operator_type'];
                    $cityId = empty($data['city_id']) ? 0 : (int)$data['city_id'];

                    if(!in_array($rechargeType, [1,2]) || !in_array($operatorType, [1,2,3]) || empty($cityId) || empty($data['amount'])){
                        $payload->reply(json_encode(ServerUtil::nats(1001,'请求参数不对')));
                    } else{
                        $res = $obj->amountByChannelInfo($rechargeType, $operatorType, $cityId, $data['amount']);
                        if(empty($res)){
                            $payload->reply(json_encode(ServerUtil::nats(1002,'没有可用渠道')));
                        }else{
                            $payload->reply(json_encode(ServerUtil::nats(0,'获取成功',$res)));
                        }
                        ServerUtil::info('Nats获取渠道信息', ['path'=>'sg_phonecode.channel', 'parameter'=>$data, 'result'=>$res]);

                    }
                    break;
                case 2:
                    //根据商户号获取渠道信息
                    if(empty($data['merchant_no'])){
                        $payload->reply(json_encode(ServerUtil::nats(1001,'请求参数不对')));
                    } else{
                        $res = $obj->merchantNoByChannelInfo($data['merchant_no']);
                        if($res){
                            $payload->reply(json_encode(ServerUtil::nats(0,'获取成功',$res)));
                        }else{
                            $payload->reply(json_encode(ServerUtil::nats(1002,'没有可用渠道')));
                        }
                        ServerUtil::info('Nats获取渠道信息', ['path'=>'sg_phonecode.channel', 'parameter'=>$data, 'result'=>$res]);
                    }
                    break;

                default:
                    $payload->reply(json_encode(ServerUtil::nats(600,'type解析失败')));
            }


        });
        return;
    }
}